package com.bysj.sausys.controller.backend;

import com.bysj.sausys.beans.PageQuery;
import com.bysj.sausys.beans.PageResult;
import com.bysj.sausys.common.AssConst;
import com.bysj.sausys.common.JsonData;
import com.bysj.sausys.common.UserConst;
import com.bysj.sausys.model.SysAss;
import com.bysj.sausys.service.ISysAssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 社团Controller
 *
 * @author WangZK
 */
@Api(description = "社团接口")
@Controller
@RequestMapping("/sys/ass")
public class SysAssController {

    @Resource
    private ISysAssService sysAssService;

    /**
     * 保存社团信息
     *
     * @param sysAss
     * @param request
     * @return
     */
    @ApiOperation(value = "保存社团信息接口（超级管理员）")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public JsonData save(@ApiParam(value = "社团实体类", required = true) @RequestBody SysAss sysAss,
                            HttpServletRequest request) {
        // 从session中获取当前用户
        Map map = UserConst.getCurrentUser(request);
        sysAssService.save(sysAss, UserConst.getCurrentUsername(map));
        return JsonData.success();
    }

    /**
     * 更新社团信息
     *
     * @param sysAss
     * @param request
     * @return
     */
    @ApiOperation(value = "更新社团信息接口（超级管理员）")
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    @ResponseBody
    public JsonData update(@ApiParam(value = "社团实体类", required = true) @RequestBody SysAss sysAss,
                              HttpServletRequest request) {
        // 从session中获取当前用户
        Map map = UserConst.getCurrentUser(request);
        sysAssService.update(sysAss, UserConst.getCurrentUsername(map));
        return JsonData.success();
    }

    /**
     * 获取社团列表
     *
     * @param keyword
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "获取社团列表（超级管理员）")
    @RequestMapping(value = "/getPage", method = RequestMethod.GET)
    @ResponseBody
    public JsonData getPage(@ApiParam(value = "关键词") @RequestParam(required = false) String keyword,
                            @ApiParam(value = "页码") @RequestParam(required = false, defaultValue = "1") int pageNo,
                            @ApiParam(value = "每页显示条数") @RequestParam(required = false, defaultValue = "10") int pageSize) {
        PageResult result = sysAssService.getPageByKeyword(keyword, new PageQuery(pageNo, pageSize));
        return JsonData.success(result);
    }

    /**
     * 根据id获取社团信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id获取社团信息接口")
    @RequestMapping(value = "/getOne", method = RequestMethod.GET)
    @ResponseBody
    public JsonData getOne(@ApiParam(value = "社团id", required = true) @RequestParam Integer id) {
        SysAss sysAss = sysAssService.findById(id);
        return JsonData.success(sysAss);
    }

    /**
     * 获取所有社团
     *
     * @return
     */
    @ApiOperation(value = "获取所有社团的接口")
    @RequestMapping(value = "/getAllIdAndName", method = RequestMethod.GET)
    @ResponseBody
    public JsonData getAllIdAndName() {
        List<Map> list = sysAssService.getAllIdAndNameAndStatus(AssConst.ENROLL.getCode());
        return JsonData.success(list);
    }

}
